import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA
 * Description:泛型
 * User: lv
 * Date: 2023-03-17
 * Time: 15:17
 */
class MyArray1{
    public Object[] objects=new Object[10];

    public void set(int pos,Object val){
        objects[pos]=val;
    }

    public Object get(int pos){
        return objects[pos];
    }

}

/**
 *
 * @param <T> 此时代表当前类是一个泛型类 T:当作是一个占位符
 */
class MyArray<T>{
    //public T[] objects=new T[10];//ERROR 1.不能实例化泛型类型的数组
    public T[] objects= (T[]) new Object[10];//也不可以

    public void set(int pos,T val){
        objects[pos]=val;
    }

    public T get(int pos){
        return objects[pos];
    }

}


//写一个泛型类，求出数组当中的最大值
class Alg<T extends Comparable<T>>{
    public T findMax(T[] array){
        T max=array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i])<0){
                max=array[i];
            }
        }
        return max;
    }
}

class Alg2 {
    public static<T extends Comparable<T>> T findMax(T[] array){
        T max=array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i])<0){
                max=array[i];
            }
        }
        return max;
    }
}

class Alg3{
    public static <T> void print1(ArrayList<T> list){
        for (T x: list) {
            System.out.println(x);
        }
    }
    public static void print2(ArrayList<?> list){
        for (Object x:list) {
            System.out.println(x);
        }
    }
}

public class TestDemo {

    public static void main(String[] args) {
        ArrayList<Integer> list1=new ArrayList<>();
        list1.add(1);
        list1.add(3);
        Alg3.print1(list1);
        System.out.println("==================");
        Alg3.print2(list1);
    }

    public static void main4(String[] args) {
        Integer[] array={1,2,3,14,5};
        System.out.println(Alg2.findMax(array));
    }

    public static void main3(String[] args) {
        Alg<Integer> alg=new Alg<>();
        Integer[] array={1,2,13,4,5};
        System.out.println(alg.findMax(array));
    }

    public static void main2(String[] args) {
        MyArray<String> myArray=new MyArray<>();
        myArray.set(0,"java");
        //myArray.set(1,12);//编译的时候，自动进行类型的检查
        String str= myArray.get(0);//不需要进行强制类型的转换

        MyArray<Integer> myArray2=new MyArray<>();
        myArray2.set(0,1);
        //MyArray<int> myArray2=new MyArray<>();//简单类型 基本类型不能作为泛型的参数
    }

    public static void main1(String[] args) {
        MyArray1 myArray=new MyArray1();
        myArray.set(0,"java");
        myArray.set(1,12);
        String str=(String) myArray.get(0);
    }
}
